package org.boot.easyapi.core.controller.view.table;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.boot.easyapi.core.controller.view.table.req.TableViewVO;
import org.boot.easyapi.core.service.table.TableViewService;
import org.boot.easyapi.pojo.db.DbDTO;
import org.boot.easyapi.pojo.db.TableDTO;
import org.boot.easyapi.pojo.web.CommonResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

import java.util.List;
import java.util.Map;

import static org.boot.easyapi.pojo.web.CommonResult.success;


@RestController
@RequestMapping("/easyapi/table-view")
@Tag(name = "View数据 - 表结构管理")
@Validated
@Slf4j
public class TableViewController {
    @Resource(name = "easyapi-tableViewService")
    private TableViewService tableViewService;

    @PostMapping("/create-table")
    @Operation(summary = "创建数据表结构")
    public CommonResult<Boolean> createTable(@RequestBody TableViewVO viewVO){
        tableViewService.create(viewVO);
        return success(true);
    }

    @GetMapping("/get-db")
    @Operation(summary = "获取数据库类型")
    public CommonResult<DbDTO> getDbType(){
        DbDTO dbType = tableViewService.getDbType();
        dbType.setDbUrl(null);
        dbType.setDbUserName(null);
        return success(dbType);
    }

    @GetMapping("/get-table-list")
    @Operation(summary = "获取表结构列表")
    public CommonResult<Map<String, List<TableDTO>>> getTableList(){
        return success(tableViewService.getTableList());
    }

    @GetMapping("/get-table")
    @Operation(summary = "获取指定表结构")
    public CommonResult<List<TableDTO>> getTable(@RequestParam("tableName") String tableName){
        return success(tableViewService.getTable(tableName));
    }





}
